#!/bin/bash
if [[ $# == 3 ]]; then
	errors=""
	if [[ ! $1 =~ ([[:alnum:]]+\.)+[[:alnum:]]+ ]]; then
		errors="Invalid target class name: $1\n"
	fi
	if [[ ! -d "$2" ]]; then
		errors="${errors}Not a ginr output directory: $2\n"
	fi

	if [[ -z "$errors" ]]; then
		if [[ -e "$3" ]]; then
			rm -f "$3"
		elif [[ ! -d $(dirname "$3") ]]; then
			mkdir $(dirname "$3")
		fi
		rc=$?
		if [[ $rc -ne 0 ]]; then
			errors="Unable to [re]create output file: $3\n"
		else
			indir=$(readlink -f $2)
			outfile==$(dirname $(readlink -f $3))/$(basename $3)
			pushd $(dirname $(readlink -f $0)) > /dev/null
			java -cp jrte-HEAD.jar com.characterforming.jrte.compile.GearboxCompiler --target $1 "$indir" "$outfile"
			rc=$?
			popd > /dev/null
			exit $rc
		fi
	fi

	echo -e "$errors"
	exit 1
else
	echo -e "Usage: jtrc.sh target-class-name ginr-output-dirpath jrte-gearbox-filepath\n"
	exit 1
fi